{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1",
   "metadata": {},
   "source": [
    "# Secondary Market Research Agents\n",
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/MervinPraison/PraisonAI/blob/main/cookbooks/notebooks/secondary_market_research_agents.ipynb)\n",
    "\n",
    "This notebook demonstrates how to create a comprehensive secondary market research system using PraisonAI agents. The system generates detailed reports with customizable parameters for company, geography, and industry analysis."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2", 
   "metadata": {},
   "source": [
    "## Features\n",
    "\n",
    "- **Multi-agent Research System**: Specialized agents for different research areas\n",
    "- **Customizable Parameters**: Company, geography, industry, and research sections\n",
    "- **Comprehensive Reports**: Market overview, competitive analysis, financial performance, growth opportunities, and risk assessment\n",
    "- **FastAPI Integration**: Ready for production deployment\n",
    "- **Structured Output**: Professional report format suitable for business decision-making"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## Installation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install required dependencies\n",
    "%pip install praisonai[crewai] > /dev/null\n",
    "%pip install fastapi uvicorn > /dev/null\n",
    "%pip install requests > /dev/null"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "## Setup API Key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from google.colab import userdata\n",
    "\n",
    "# Set up OpenAI API key (add to Google Colab secrets)\n",
    "os.environ[\"OPENAI_API_KEY\"] = userdata.get('OPENAI_API_KEY') or \"ENTER OPENAI_API_KEY HERE\"\n",
    "os.environ[\"OPENAI_MODEL_NAME\"] = \"gpt-5-nano\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "## Method 1: Using YAML Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# YAML configuration for market research agents\n",
    "agent_yaml = \"\"\"\n",
    "framework: \"crewai\"\n",
    "topic: \"Secondary Market Research Analysis\"\n",
    "\n",
    "# Configuration variables\n",
    "variables:\n",
    "  company: \"Tesla\"\n",
    "  geography: \"North America\" \n",
    "  industry: \"Electric Vehicles\"\n",
    "\n",
    "roles:\n",
    "  market_overview_specialist:\n",
    "    role: \"Market Overview Specialist\" \n",
    "    backstory: |\n",
    "      You are a seasoned market research analyst with 10+ years of experience \n",
    "      in analyzing market dynamics, trends, and growth patterns.\n",
    "    goal: |\n",
    "      Analyze the {industry} market in {geography} to provide comprehensive \n",
    "      market overview including size, trends, and growth drivers.\n",
    "    verbose: true\n",
    "    allow_delegation: false\n",
    "    tools:\n",
    "      - \"InternetSearchTool\"\n",
    "    tasks:\n",
    "      market_overview_research:\n",
    "        description: |\n",
    "          Conduct comprehensive market overview research for {industry} in {geography}:\n",
    "          1. Market size and valuation analysis\n",
    "          2. Key growth trends and patterns\n",
    "          3. Primary market drivers\n",
    "          4. Market segmentation\n",
    "        expected_output: |\n",
    "          A comprehensive market overview report with quantitative data and insights.\n",
    "\n",
    "  competitive_intelligence_analyst:\n",
    "    role: \"Competitive Intelligence Analyst\"\n",
    "    backstory: |\n",
    "      You are a competitive intelligence expert specializing in analyzing \n",
    "      competitive landscapes and market positioning.\n",
    "    goal: |\n",
    "      Analyze {company}'s competitive landscape in the {industry} market.\n",
    "    verbose: true\n",
    "    allow_delegation: false\n",
    "    tools:\n",
    "      - \"InternetSearchTool\"\n",
    "    tasks:\n",
    "      competitive_analysis:\n",
    "        description: |\n",
    "          Conduct detailed competitive analysis for {company}:\n",
    "          1. Identify top 5-7 competitors\n",
    "          2. Market share analysis\n",
    "          3. Competitive positioning\n",
    "          4. Strengths and weaknesses\n",
    "        expected_output: |\n",
    "          A comprehensive competitive intelligence report.\n",
    "\n",
    "  financial_performance_analyst:\n",
    "    role: \"Financial Performance Analyst\"\n",
    "    backstory: |\n",
    "      You are a financial analyst with expertise in corporate finance \n",
    "      and industry benchmarking.\n",
    "    goal: |\n",
    "      Analyze {company}'s financial performance and industry benchmarks.\n",
    "    verbose: true\n",
    "    allow_delegation: false\n",
    "    tools:\n",
    "      - \"InternetSearchTool\"\n",
    "    tasks:\n",
    "      financial_analysis:\n",
    "        description: |\n",
    "          Conduct financial performance analysis for {company}:\n",
    "          1. Revenue trends and growth\n",
    "          2. Profitability metrics\n",
    "          3. Financial ratios\n",
    "          4. Industry benchmarking\n",
    "        expected_output: |\n",
    "          A detailed financial performance analysis report.\n",
    "\n",
    "  research_report_synthesizer:\n",
    "    role: \"Research Report Synthesizer\"\n",
    "    backstory: |\n",
    "      You are an expert business report writer specializing in \n",
    "      synthesizing complex research into actionable insights.\n",
    "    goal: |\n",
    "      Create a comprehensive secondary market research report for {company}.\n",
    "    verbose: true\n",
    "    allow_delegation: false\n",
    "    tasks:\n",
    "      report_synthesis:\n",
    "        description: |\n",
    "          Synthesize all research findings into a comprehensive report:\n",
    "          1. Executive Summary\n",
    "          2. Market Overview\n",
    "          3. Competitive Analysis\n",
    "          4. Financial Performance\n",
    "          5. Conclusions and Recommendations\n",
    "        expected_output: |\n",
    "          A complete, professionally formatted secondary market research report.\n",
    "        context:\n",
    "          - \"market_overview_research\"\n",
    "          - \"competitive_analysis\" \n",
    "          - \"financial_analysis\"\n",
    "\n",
    "dependencies:\n",
    "  - market_overview_specialist\n",
    "  - competitive_intelligence_analyst\n",
    "  - financial_performance_analyst\n",
    "  - research_report_synthesizer\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import required libraries\n",
    "from praisonai import PraisonAI\n",
    "from praisonai_tools import BaseTool\n",
    "from duckduckgo_search import DDGS\n",
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "# Create internet search tool\n",
    "class InternetSearchTool(BaseTool):\n",
    "    name: str = \"InternetSearchTool\"\n",
    "    description: str = \"Search Internet for relevant information\"\n",
    "\n",
    "    def _run(self, query: str):\n",
    "        ddgs = DDGS()\n",
    "        results = ddgs.text(keywords=query, region='wt-wt', safesearch='moderate', max_results=5)\n",
    "        return results\n",
    "\n",
    "# Create PraisonAI instance\n",
    "praisonai = PraisonAI(agent_yaml=agent_yaml, tools=[InternetSearchTool])\n",
    "\n",
    "# Run the market research\n",
    "print(\"🔍 Starting Secondary Market Research for Tesla...\")\n",
    "result = praisonai.run()\n",
    "\n",
    "print(\"\\n📋 Market Research Complete!\")\n",
    "print(\"=\" * 60)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "## Method 2: Using Python API with Custom Parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the market research system\n",
    "from praisonaiagents import Agent, Task, PraisonAIAgents, Tools\n",
    "from typing import Dict, List, Any\n",
    "import asyncio\n",
    "from datetime import datetime\n",
    "\n",
    "class MarketResearchConfig:\n",
    "    \"\"\"Configuration for market research parameters\"\"\"\n",
    "    \n",
    "    def __init__(self, company: str, geography: str, industry: str, sections: List[str]):\n",
    "        self.company = company\n",
    "        self.geography = geography\n",
    "        self.industry = industry\n",
    "        self.sections = sections\n",
    "        self.timestamp = datetime.now().isoformat()\n",
    "\n",
    "def create_market_research_agents(config: MarketResearchConfig):\n",
    "    \"\"\"Create specialized market research agents\"\"\"\n",
    "    \n",
    "    agents = {}\n",
    "    \n",
    "    if \"market_overview\" in config.sections:\n",
    "        agents[\"market_overview\"] = Agent(\n",
    "            name=\"Market Overview Specialist\",\n",
    "            role=\"Market Analysis Expert\",\n",
    "            goal=f\"Analyze the {config.industry} market in {config.geography}\",\n",
    "            instructions=f\"\"\"\n",
    "            Analyze market size, trends, growth drivers for {config.industry} in {config.geography}.\n",
    "            Provide quantitative data and cite reliable sources.\n",
    "            \"\"\",\n",
    "            tools=[Tools.internet_search],\n",
    "            verbose=True\n",
    "        )\n",
    "    \n",
    "    if \"competitive_analysis\" in config.sections:\n",
    "        agents[\"competitive\"] = Agent(\n",
    "            name=\"Competitive Intelligence Analyst\",\n",
    "            role=\"Competition Research Expert\", \n",
    "            goal=f\"Analyze {config.company}'s competitive landscape\",\n",
    "            instructions=f\"\"\"\n",
    "            Analyze {config.company}'s competitive position in {config.industry}.\n",
    "            Identify key competitors, market share, and competitive advantages.\n",
    "            \"\"\",\n",
    "            tools=[Tools.internet_search],\n",
    "            verbose=True\n",
    "        )\n",
    "    \n",
    "    if \"financial_performance\" in config.sections:\n",
    "        agents[\"financial\"] = Agent(\n",
    "            name=\"Financial Performance Analyst\", \n",
    "            role=\"Financial Research Expert\",\n",
    "            goal=f\"Analyze {config.company}'s financial performance\",\n",
    "            instructions=f\"\"\"\n",
    "            Research {config.company}'s financial performance, revenue trends,\n",
    "            profitability, and compare with industry benchmarks.\n",
    "            \"\"\",\n",
    "            tools=[Tools.internet_search],\n",
    "            verbose=True\n",
    "        )\n",
    "    \n",
    "    if \"growth_opportunities\" in config.sections:\n",
    "        agents[\"growth\"] = Agent(\n",
    "            name=\"Growth Opportunities Researcher\",\n",
    "            role=\"Strategic Growth Expert\",\n",
    "            goal=f\"Identify growth opportunities for {config.company}\",\n",
    "            instructions=f\"\"\"\n",
    "            Identify emerging opportunities, market gaps, expansion possibilities\n",
    "            for {config.company} in {config.industry}.\n",
    "            \"\"\",\n",
    "            tools=[Tools.internet_search],\n",
    "            verbose=True\n",
    "        )\n",
    "    \n",
    "    # Always include synthesizer\n",
    "    agents[\"synthesizer\"] = Agent(\n",
    "        name=\"Research Report Synthesizer\",\n",
    "        role=\"Report Writing Expert\",\n",
    "        goal=\"Synthesize research into comprehensive report\",\n",
    "        instructions=f\"\"\"\n",
    "        Create a professional secondary market research report for {config.company} \n",
    "        in {config.industry} with clear structure and actionable recommendations.\n",
    "        \"\"\",\n",
    "        verbose=True\n",
    "    )\n",
    "    \n",
    "    return agents\n",
    "\n",
    "def create_research_tasks(agents: Dict[str, Agent], config: MarketResearchConfig):\n",
    "    \"\"\"Create research tasks for the agents\"\"\"\n",
    "    \n",
    "    tasks = []\n",
    "    \n",
    "    if \"market_overview\" in config.sections and \"market_overview\" in agents:\n",
    "        market_task = Task(\n",
    "            name=\"market_overview_research\",\n",
    "            description=f\"Research {config.industry} market overview in {config.geography}\",\n",
    "            expected_output=\"Market overview analysis with key metrics and trends\",\n",
    "            agent=agents[\"market_overview\"]\n",
    "        )\n",
    "        tasks.append(market_task)\n",
    "    \n",
    "    if \"competitive_analysis\" in config.sections and \"competitive\" in agents:\n",
    "        competitive_task = Task(\n",
    "            name=\"competitive_analysis\",\n",
    "            description=f\"Analyze competitive landscape for {config.company}\",\n",
    "            expected_output=\"Competitive analysis with key competitors and positioning\",\n",
    "            agent=agents[\"competitive\"]\n",
    "        )\n",
    "        tasks.append(competitive_task)\n",
    "    \n",
    "    if \"financial_performance\" in config.sections and \"financial\" in agents:\n",
    "        financial_task = Task(\n",
    "            name=\"financial_analysis\",\n",
    "            description=f\"Analyze {config.company}'s financial performance\",\n",
    "            expected_output=\"Financial performance analysis with key metrics\",\n",
    "            agent=agents[\"financial\"]\n",
    "        )\n",
    "        tasks.append(financial_task)\n",
    "    \n",
    "    if \"growth_opportunities\" in config.sections and \"growth\" in agents:\n",
    "        growth_task = Task(\n",
    "            name=\"growth_opportunities\",\n",
    "            description=f\"Identify growth opportunities for {config.company}\",\n",
    "            expected_output=\"Growth opportunities with strategic recommendations\",\n",
    "            agent=agents[\"growth\"]\n",
    "        )\n",
    "        tasks.append(growth_task)\n",
    "    \n",
    "    # Synthesis task\n",
    "    synthesis_task = Task(\n",
    "        name=\"synthesis\",\n",
    "        description=\"Synthesize all findings into comprehensive report\",\n",
    "        expected_output=\"Complete secondary market research report\",\n",
    "        agent=agents[\"synthesizer\"],\n",
    "        context=tasks\n",
    "    )\n",
    "    tasks.append(synthesis_task)\n",
    "    \n",
    "    return tasks\n",
    "\n",
    "async def run_market_research(config: MarketResearchConfig):\n",
    "    \"\"\"Run the complete market research workflow\"\"\"\n",
    "    \n",
    "    print(f\"🔍 Starting Market Research for {config.company}\")\n",
    "    print(f\"📍 Geography: {config.geography}\")\n",
    "    print(f\"🏭 Industry: {config.industry}\")\n",
    "    print(f\"📊 Sections: {', '.join(config.sections)}\")\n",
    "    print(\"=\" * 60)\n",
    "    \n",
    "    # Create agents and tasks\n",
    "    agents = create_market_research_agents(config)\n",
    "    tasks = create_research_tasks(agents, config)\n",
    "    \n",
    "    # Create workflow\n",
    "    workflow = PraisonAIAgents(\n",
    "        agents=list(agents.values()),\n",
    "        tasks=tasks,\n",
    "        process=\"workflow\",\n",
    "        verbose=True\n",
    "    )\n",
    "    \n",
    "    # Execute research\n",
    "    results = await workflow.astart()\n",
    "    \n",
    "    return results\n",
    "\n",
    "# Example with custom parameters\n",
    "custom_config = MarketResearchConfig(\n",
    "    company=\"Apple\",\n",
    "    geography=\"Global\", \n",
    "    industry=\"Consumer Electronics\",\n",
    "    sections=[\"market_overview\", \"competitive_analysis\", \"financial_performance\"]\n",
    ")\n",
    "\n",
    "# Run the research\n",
    "print(\"🚀 Running Custom Market Research...\")\n",
    "custom_results = await run_market_research(custom_config)\n",
    "\n",
    "print(\"\\n📋 Research Complete!\")\n",
    "print(\"=\" * 60)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "## Method 3: FastAPI Integration Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example of using the FastAPI endpoint\n",
    "import requests\n",
    "import json\n",
    "import time\n",
    "\n",
    "# Note: This requires the FastAPI server to be running\n",
    "# Run: uvicorn secondary-market-research-api:app --reload --port 8000\n",
    "\n",
    "API_BASE_URL = \"http://localhost:8000\"  # Change if running elsewhere\n",
    "\n",
    "def generate_report_via_api(company, geography, industry, sections):\n",
    "    \"\"\"Generate market research report via API\"\"\"\n",
    "    \n",
    "    # Prepare request\n",
    "    request_data = {\n",
    "        \"company\": company,\n",
    "        \"geography\": geography,\n",
    "        \"industry\": industry,\n",
    "        \"sections\": sections,\n",
    "        \"format\": \"json\"\n",
    "    }\n",
    "    \n",
    "    print(f\"📤 Submitting research request for {company}...\")\n",
    "    \n",
    "    # Submit research request\n",
    "    response = requests.post(f\"{API_BASE_URL}/research/generate\", json=request_data)\n",
    "    \n",
    "    if response.status_code == 200:\n",
    "        job_data = response.json()\n",
    "        job_id = job_data[\"job_id\"]\n",
    "        print(f\"✅ Job submitted successfully. Job ID: {job_id}\")\n",
    "        \n",
    "        # Poll for completion\n",
    "        print(\"⏳ Waiting for completion...\")\n",
    "        while True:\n",
    "            status_response = requests.get(f\"{API_BASE_URL}/research/status/{job_id}\")\n",
    "            if status_response.status_code == 200:\n",
    "                status_data = status_response.json()\n",
    "                print(f\"📊 Progress: {status_data['progress']}% - {status_data['message']}\")\n",
    "                \n",
    "                if status_data[\"status\"] == \"completed\":\n",
    "                    print(\"🎉 Research completed!\")\n",
    "                    \n",
    "                    # Download report\n",
    "                    report_response = requests.get(f\"{API_BASE_URL}/research/reports/{job_id}\")\n",
    "                    if report_response.status_code == 200:\n",
    "                        return report_response.json()\n",
    "                    else:\n",
    "                        print(\"❌ Failed to download report\")\n",
    "                        return None\n",
    "                        \n",
    "                elif status_data[\"status\"] == \"failed\":\n",
    "                    print(f\"❌ Research failed: {status_data.get('error', 'Unknown error')}\")\n",
    "                    return None\n",
    "                    \n",
    "                time.sleep(5)  # Wait 5 seconds before checking again\n",
    "            else:\n",
    "                print(\"❌ Failed to check status\")\n",
    "                return None\n",
    "    else:\n",
    "        print(f\"❌ Failed to submit request: {response.status_code}\")\n",
    "        return None\n",
    "\n",
    "# Example API usage (uncomment to run with API server)\n",
    "# report = generate_report_via_api(\n",
    "#     company=\"Microsoft\",\n",
    "#     geography=\"Global\",\n",
    "#     industry=\"Cloud Computing\",\n",
    "#     sections=[\"market_overview\", \"competitive_analysis\"]\n",
    "# )\n",
    "# \n",
    "# if report:\n",
    "#     print(\"\\n📋 Generated Report:\")\n",
    "#     print(json.dumps(report, indent=2)[:1000] + \"...\")\n",
    "\n",
    "print(\"💡 API integration example ready. Start the FastAPI server to test.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14",
   "metadata": {},
   "source": [
    "## Customization Examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example configurations for different industries and regions\n",
    "\n",
    "research_examples = {\n",
    "    \"tech_startup\": {\n",
    "        \"company\": \"OpenAI\",\n",
    "        \"geography\": \"United States\",\n",
    "        \"industry\": \"Artificial Intelligence\",\n",
    "        \"sections\": [\"market_overview\", \"competitive_analysis\", \"growth_opportunities\"]\n",
    "    },\n",
    "    \"automotive\": {\n",
    "        \"company\": \"BMW\",\n",
    "        \"geography\": \"Europe\",\n",
    "        \"industry\": \"Luxury Automobiles\",\n",
    "        \"sections\": [\"market_overview\", \"competitive_analysis\", \"financial_performance\", \"risk_assessment\"]\n",
    "    },\n",
    "    \"healthcare\": {\n",
    "        \"company\": \"Pfizer\",\n",
    "        \"geography\": \"Global\",\n",
    "        \"industry\": \"Pharmaceuticals\",\n",
    "        \"sections\": [\"market_overview\", \"competitive_analysis\", \"growth_opportunities\", \"risk_assessment\"]\n",
    "    },\n",
    "    \"fintech\": {\n",
    "        \"company\": \"Square\",\n",
    "        \"geography\": \"North America\",\n",
    "        \"industry\": \"Financial Technology\",\n",
    "        \"sections\": [\"market_overview\", \"competitive_analysis\", \"financial_performance\", \"growth_opportunities\"]\n",
    "    },\n",
    "    \"retail\": {\n",
    "        \"company\": \"Alibaba\",\n",
    "        \"geography\": \"Asia Pacific\",\n",
    "        \"industry\": \"E-commerce\",\n",
    "        \"sections\": [\"market_overview\", \"competitive_analysis\", \"financial_performance\", \"growth_opportunities\", \"risk_assessment\"]\n",
    "    }\n",
    "}\n",
    "\n",
    "print(\"📚 Research Configuration Examples:\")\n",
    "print(\"=\" * 50)\n",
    "\n",
    "for example_name, config in research_examples.items():\n",
    "    print(f\"\\n🔍 {example_name.upper()}:\")\n",
    "    print(f\"   Company: {config['company']}\")\n",
    "    print(f\"   Geography: {config['geography']}\")\n",
    "    print(f\"   Industry: {config['industry']}\")\n",
    "    print(f\"   Sections: {', '.join(config['sections'])}\")\n",
    "\n",
    "print(\"\\n💡 Use any of these configurations as templates for your research!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "This notebook demonstrates three different ways to use the Secondary Market Research Agent system:\n",
    "\n",
    "1. **YAML Configuration**: Easy to customize and modify research parameters\n",
    "2. **Python API**: Programmatic control with custom configurations\n",
    "3. **FastAPI Integration**: Production-ready REST API for web applications\n",
    "\n",
    "### Key Features:\n",
    "- **Customizable Research Sections**: Choose from market overview, competitive analysis, financial performance, growth opportunities, and risk assessment\n",
    "- **Geographic Flexibility**: Research any geographic region\n",
    "- **Industry Agnostic**: Works across different industries and sectors\n",
    "- **Professional Reports**: Generate business-ready research reports\n",
    "- **Scalable Architecture**: Multi-agent system that can be extended\n",
    "\n",
    "### Use Cases:\n",
    "- Market entry analysis\n",
    "- Competitive intelligence\n",
    "- Investment research\n",
    "- Strategic planning\n",
    "- Due diligence\n",
    "\n",
    "The system provides a comprehensive foundation for secondary market research that can be customized and extended based on specific business needs."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}